﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UploadClient.Interfaces.Plugins;
using UploadClient.Interfaces.Upload;

namespace UploadClient
{
    /// <summary>
    /// Serializeable Object that manages the program options.
    /// </summary>
    public class Options : IXmlSerializable 
    {
#if (DEBUG)
        private static readonly string path = Path.GetDirectoryName(Application.ExecutablePath);
#else
        //only in Releases
        private static string path = string.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
                                                               Assembly.GetExecutingAssembly().GetName().Name.ToString());
#endif

        public Options()
        {

        }

        public List<IPluginConfig> PcPluginConfig { get; set; }
        public List<IUploadConfig> PcUploadConfig { get; set; }

        private readonly string filename = LocalizationManager.ResourceManager.GetString("DAT_0003");

        #region Option params
        
        public bool IsUrlToClipboardCheckBox = false;
        public bool IsMonitoringClipboardCheckboxChecked = false;
        public bool IsQuickUploadCheckBoxChecked = false;
        public string SelectedLanguage = "en";

        #endregion

        /// <summary>
        /// Constructor with deserialization.
        /// </summary>
        /// <param name="deSerialize">If true it tryes to deserialize the optionfile.</param>
        public Options(bool deSerialize = false)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            if (File.Exists(string.Format("{0}\\{1}", path, filename)))
            {
                //deserialize
                XmlSerializer deserializer = new XmlSerializer(typeof(Options));
                TextReader textReader = new StreamReader(string.Format("{0}\\{1}", path, filename));
                Options options = (Options)deserializer.Deserialize(textReader);
                textReader.Close();

                this.IsMonitoringClipboardCheckboxChecked = options.IsMonitoringClipboardCheckboxChecked;
                this.IsUrlToClipboardCheckBox = options.IsUrlToClipboardCheckBox;
                this.IsQuickUploadCheckBoxChecked = options.IsQuickUploadCheckBoxChecked;
                this.SelectedLanguage = options.SelectedLanguage;
            }

            PcPluginConfig = new List<IPluginConfig>();
            PcUploadConfig = new List<IUploadConfig>();
        }


        /// <summary>
        /// Serialize the Object on dispose.
        /// </summary>
        ~Options()
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            //serialize
            XmlSerializer serializer = new XmlSerializer(typeof(Options));
            TextWriter textWriter = new StreamWriter(string.Format("{0}\\{1}", path, filename));
            serializer.Serialize(textWriter, this);
            textWriter.Close();
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Reads the XML into the object.
        /// </summary>
        /// <param name="reader"></param>
        public void ReadXml(XmlReader reader)
        {
            while (reader.Read())
            {
                try
                {
                    if (reader.NodeType.Equals(XmlNodeType.Element))
                    {
                        if (reader.Name.Equals("IsUrlToClipboardCheckBox"))
                            IsUrlToClipboardCheckBox = reader.ReadElementContentAsString().Equals("True") ? true : false;
                        if (reader.Name.Equals("IsMonitoringClipboardCheckboxChecked"))
                            IsMonitoringClipboardCheckboxChecked = reader.ReadElementContentAsString().Equals("True") ? true : false;
                        if (reader.Name.Equals("IsQuickUploadCheckBoxChecked"))
                            IsQuickUploadCheckBoxChecked = reader.ReadElementContentAsString().Equals("True") ? true : false;
                        if (reader.Name.Equals("SelectedLanguage"))
                            SelectedLanguage = reader.ReadElementContentAsString();
                    }
                }
                catch (Exception ex)
                {
                    new Exception(string.Format(LocalizationManager.ResourceManager.GetString("ERR_0003"), path, filename), ex);
                }
            }
        }

        /// <summary>
        /// Writes to the XML.
        /// </summary>
        /// <param name="writer">The XmlWriter.</param>
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteElementString("IsUrlToClipboardCheckBox", IsUrlToClipboardCheckBox.ToString());
            writer.WriteElementString("IsMonitoringClipboardCheckboxChecked", IsMonitoringClipboardCheckboxChecked.ToString());
            writer.WriteElementString("IsQuickUploadCheckBoxChecked", IsQuickUploadCheckBoxChecked.ToString());
            writer.WriteElementString("SelectedLanguage", SelectedLanguage);
        }
    }
}
